package com.usian.controller;

import com.usian.UserFeign;
import com.usian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private UserFeign userFeign;
    @RequestMapping("findByConsumer")
    public User findById(){
       /* //1.到注册中心：根据服务名获取服务的ip和端口
        ServiceInstance instance = loadBalancerClient.choose("provider");
        //2.拼接请求地址
        String url="http://"+instance.getHost()+":"+instance.getPort()+"findById/10";
        //3.发起请求
        User user = restTemplate.getForObject(url, User.class);*/
       User user=userFeign.findById(10);
        return user;
    }

    @RequestMapping("findByConsumer1")

    public User findById(@RequestParam int id){

       User user=userFeign.findByIds(id);
        return user;
    }
    @RequestMapping("findByConsumer2")
    public User findByConsumer2(){
        User user=new User();
        user.setId(13);
        User user1=userFeign.findByConsumer2(user);
        return user1;

    }
}
